package com.glink.manage.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.BaseCommonUtils;
import com.glink.manage.domain.PersonViolationRegulationsRecordBean;
import com.glink.manage.dto.person.PersonViolationRegulationsRecordIdDTO;
import com.glink.manage.dto.person.PersonViolationRegulationsRecordPageDTO;
import com.glink.manage.dto.person.PersonViolationRegulationsRecordSaveDTO;
import com.glink.manage.mapper.PersonViolationRegulationsRecordMapper;
import com.glink.manage.service.PersonViolationRegulationsRecordService;
import com.glink.manage.vo.person.PersonViolationRegulationsRecordVO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author  qiushaoshan
 * @since 2025-01-13
 */
@Service
@Slf4j
public class PersonViolationRegulationsRecordServiceImpl extends ServiceImpl<PersonViolationRegulationsRecordMapper, PersonViolationRegulationsRecordBean> implements PersonViolationRegulationsRecordService {

    /**
     * 根据用户ID查询违章记录
     *
     * @param dto 用户ID
     * @return 违章记录
     */
    @Override
    public IPage<PersonViolationRegulationsRecordVO> pageByViolationRegulationsRecord(PersonViolationRegulationsRecordPageDTO dto) {
        IPage<PersonViolationRegulationsRecordBean> constructionWorkBeanIPage = new Page<>(dto.getPage(), dto.getSize());

        IPage<PersonViolationRegulationsRecordBean> workBeanIPage = this.lambdaQuery()
                .eq(PersonViolationRegulationsRecordBean::getPersonObjId, dto.getPersonObjId())
                .orderByDesc(PersonViolationRegulationsRecordBean::getTime)
                .orderByDesc(PersonViolationRegulationsRecordBean::getCreateTime)
                .orderByDesc(PersonViolationRegulationsRecordBean::getObjId)
                .page(constructionWorkBeanIPage);

        IPage<PersonViolationRegulationsRecordVO> pageVOIPage = new Page<>(workBeanIPage.getCurrent(), workBeanIPage.getSize(), workBeanIPage.getTotal());
        List<PersonViolationRegulationsRecordVO> pageVOList = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(workBeanIPage.getRecords())) {
            workBeanIPage.getRecords().forEach(violationRegulationsRecordBean -> {
                PersonViolationRegulationsRecordVO recordVO = new PersonViolationRegulationsRecordVO();
                BeanUtils.copyProperties(violationRegulationsRecordBean, recordVO);
                pageVOList.add(recordVO);
            });
            pageVOIPage.setRecords(pageVOList);
        }
        return pageVOIPage;
    }

    /**
     * 新增违章记录
     *
     * @param dto 违章记录信息
     * @return 新增状态
     */
    @Override
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean saveViolationRegulationsRecord(PersonViolationRegulationsRecordSaveDTO dto) throws GeneralSimpleException {

        if(StringUtils.isBlank(dto.getPersonObjId())){
            throw new GeneralSimpleException("400038");
        }
        
        PersonViolationRegulationsRecordBean violationRegulationsRecordBean = new PersonViolationRegulationsRecordBean();
        violationRegulationsRecordBean.setObjId(BaseCommonUtils.generateUUID());
        violationRegulationsRecordBean.setBehavior(dto.getBehavior());
        violationRegulationsRecordBean.setTime(dto.getTime());
        violationRegulationsRecordBean.setPersonObjId(dto.getPersonObjId());
        
        return save(violationRegulationsRecordBean);
    }

    /**
     * 删除违章记录
     *
     * @param dto 违章记录ID
     * @return 删除状态
     */
    @Override
    @Transactional(value = "transactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean delViolationRegulationsRecord(PersonViolationRegulationsRecordIdDTO dto) {
        PersonViolationRegulationsRecordBean violationRegulationsRecordBean = this.lambdaQuery().eq(PersonViolationRegulationsRecordBean::getObjId, dto.getObjId()).one();
        if(Objects.nonNull(violationRegulationsRecordBean)){
            return removeById(dto.getObjId());
        }
        return true;
    }
}
